package com.example.AIstudy.util;

import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileUtil {

    public static void uploadFile(byte[] file,String filePath,String fileName) throws IOException {
        File targetFile=new File(filePath);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        FileOutputStream out=new FileOutputStream(targetFile.getAbsolutePath()+"/"+fileName);
        out.write(file);
        out.flush();
        out.close();
    }
    public static String getUploadFilePath(){
        File path=null;
        String filepath="api/target/classes/static/photo";
        try{
            path=new File(ResourceUtils.getURL("classpath").getPath());
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }
        System.out.println("path.getAbsolutePath()..."+path.getAbsolutePath());
        if(!path.exists()) path=new File("");
        File filePath =new File(path.getAbsolutePath(),filepath);
        return filePath.getAbsolutePath();
    }
}

